package game;

import game.console.GameConsole;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class GameListener implements KeyListener, MouseListener, MouseMotionListener, MouseWheelListener
{
    private List<KeyListener>         KeyListeners;
    private List<MouseListener>       MouseListeners;
    private List<MouseMotionListener> MouseMotionListeners;
    private List<MouseWheelListener>  MouseWheelListeners;
    private GameConsole greedyConsole;
    
    public GameListener()
    {
        KeyListeners         = Collections.synchronizedList(new ArrayList<KeyListener>());
        MouseListeners       = Collections.synchronizedList(new ArrayList<MouseListener>());
        MouseMotionListeners = Collections.synchronizedList(new ArrayList<MouseMotionListener>());
        MouseWheelListeners  = Collections.synchronizedList(new ArrayList<MouseWheelListener>());
    }
    
    public void addKeyListener(KeyListener e)
    {
        KeyListeners.add(e);
    }
    
    public void removeKeyListener(KeyListener e)
    {
        KeyListeners.remove(e);
    }
    
    public void addMouseListener(MouseListener e)
    {
        MouseListeners.add(e);
    }
    
    public void removeMouseListener(MouseListener e)
    {
        MouseListeners.remove(e);
    }
    
    public void addMouseMotionListener(MouseMotionListener e)
    {
        MouseMotionListeners.add(e);
    }
    
    public void removeMouseMotionListener(MouseMotionListener e)
    {
        MouseMotionListeners.remove(e);
    }
    
    public void addMouseWheelListener(MouseWheelListener e)
    {
        MouseWheelListeners.add(e);
    }
    
    public void removeMouseWheelListener(MouseWheelListener e)
    {
        MouseWheelListeners.remove(e);
    }
    
    @Override
    public void mouseWheelMoved(MouseWheelEvent e)
    {
        List<MouseWheelListener> ll = new ArrayList<>(MouseWheelListeners);
        for (MouseWheelListener l : ll)
        {
            l.mouseWheelMoved(e);
        }
    }
    
    @Override
    public void mouseDragged(MouseEvent e)
    {
        List<MouseMotionListener> ll = new ArrayList<>(MouseMotionListeners);
        for (MouseMotionListener l : ll)
        {
            l.mouseDragged(e);
        }
    }
    
    @Override
    public void mouseMoved(MouseEvent e)
    {
        List<MouseMotionListener> ll = new ArrayList<>(MouseMotionListeners);
        for (MouseMotionListener l : ll)
        {
            l.mouseMoved(e);
        }
    }
    
    @Override
    public void mouseClicked(MouseEvent e)
    {
        List<MouseListener> ll = new ArrayList<>(MouseListeners);
        for (MouseListener l : ll)
        {
            l.mouseClicked(e);
        }
    }
    
    @Override
    public void mousePressed(MouseEvent e)
    {
        List<MouseListener> ll = new ArrayList<>(MouseListeners);
        for (MouseListener l : ll)
        {
            l.mousePressed(e);
        }
    }
    
    @Override
    public void mouseReleased(MouseEvent e)
    {
        List<MouseListener> ll = new ArrayList<>(MouseListeners);
        for (MouseListener l : ll)
        {
            l.mouseReleased(e);
        }
    }
    
    @Override
    public void mouseEntered(MouseEvent e)
    {
        List<MouseListener> ll = new ArrayList<>(MouseListeners);
        for (MouseListener l : ll)
        {
            l.mouseEntered(e);
        }
    }
    
    @Override
    public void mouseExited(MouseEvent e)
    {
        List<MouseListener> ll = new ArrayList<>(MouseListeners);
        for (MouseListener l : ll)
        {
            l.mouseExited(e);
        }
    }
    
    @Override
    public void keyTyped(KeyEvent e)
    {
        if (greedyConsole != null && (greedyConsole.isOpen() || greedyConsole.consoleKeyPressed(e)))
        {
            greedyConsole.getIngameConsoleKeyAdapter().keyTyped(e);
        }
        else
        {
            List<KeyListener> ll = new ArrayList<>(KeyListeners);
            for (KeyListener l : ll)
            {
                l.keyTyped(e);
            }
        }
    }
    
    @Override
    public void keyPressed(KeyEvent e)
    {
        if (greedyConsole != null && (greedyConsole.isOpen() || greedyConsole.consoleKeyPressed(e)))
        {
            greedyConsole.getIngameConsoleKeyAdapter().keyPressed(e);
        }
        else
        {
            List<KeyListener> ll = new ArrayList<>(KeyListeners);
            for (KeyListener l : ll)
            {
                l.keyPressed(e);
            }
        }
    }
    
    @Override
    public void keyReleased(KeyEvent e)
    {
        if (greedyConsole != null && (greedyConsole.isOpen() || greedyConsole.consoleKeyPressed(e)))
        {
            greedyConsole.getIngameConsoleKeyAdapter().keyReleased(e);
        }
        else
        {
            List<KeyListener> ll = new ArrayList<>(KeyListeners);
            for (KeyListener l : ll)
            {
                l.keyReleased(e);
            }
        }
    }

    public void enableGreedyConsole(GameConsole gameConsole)
    {
        greedyConsole = gameConsole;
    }
}
